
/**
 * Copyright 2009 University of  Washington
 * This code is released under the GPL version 3 - with a copy of the license included
 * @author Maya Hao Li
 *
 */

Browser = navigator.appName
	Net = Browser.indexOf("Netscape")
	Micro = Browser.indexOf("Microsoft")
	Netscape = false
	IE = false

	 if(Net >= 0) {
		Netscape = true
	}

	if(Micro >= 0) {
		IE = true
	}
	

// debug messages
g_Debug = false;
function DMsg(text)
{
	if(g_Debug)
		alert(text);
}

// calendar object ****************************************************/
		var TODAY = new Date();
		var TODAY_DATE = TODAY.getDate();
		var TODAY_MONTH = TODAY.getMonth();
		var TODAY_YEAR = TODAY.getFullYear();
		var TODAY_DAY = TODAY.getDay();

		function Calendar(month, year){
			this.month = month;
			this.year = year;
			this.created = false;

			// method
			this.getCurrMonth = getCalendarMonth;
			this.getCurrYear = getCalendarYear;
			this.setCurrMonth = setCalendarMonth;
			this.setCurrYear = setCalendarYear;
			this.create = calendarCreate;

		}

		function getCalendarYear(){
			return this.year;
		}

		function getCalendarMonth(){
			return this.month;
		}

		function setCalendarMonth(month){
			this.month = month;
		}

		function setCalendarYear(year){
			this.year = year;
		}

		function calendarCreate(){
			DMsg ("entering calendarCreate");

			monthPad = new Month(this.month, this.year);		// month calendar
			var toolBar = new ToolBar(this.month, this.year);	// tool bar

			DMsg("done creating month & toolbar");
			if(this.created)
				return;

			var calendar_str = '';
			calendar_str += '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">';
			calendar_str += ' <html><head>';
			calendar_str += ' <title>Calendar</title>';
			calendar_str += ' <link href="../stylesheet/calendar.css" rel="stylesheet">';
			calendar_str += ' <script src="../javascript/calendar.js"></script>';
			calendar_str += ' </head>';
			calendar_str += ' <body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0"">';
			calendar_str += ' <div id="calendar">';
			calendar_str += toolBar.create();
			calendar_str += monthPad.create();
			DMsg("done with toolBar.create and monthPad.create()");
			calendar_str += ' <div align="center">';
			calendar_str += ' <a href="#" class="button" onClick="window.close();">Close</a></div>';
			calendar_str += ' </div>';
			calendar_str += ' </body></html>';

			document.open();
			document.write (calendar_str);
			document.close();

			DMsg("done writing html");
			this.created = true;

			DMsg ("leaving calendarCreate");
		}

// month object  ****************************************************/
		function Month(numMonth, numYear){
			this.year = numYear;
			this.month = numMonth;
			this.numDays = setNumDays(this.month, this.year);
			this.firstMonthDay = setFirstMonthDay(this.month, this.year);

			// css attributes
			this.thBgColor = '#3333ff';
			this.thColor = '#ffffff';
			this.thBorderColor = 'grey';
			this.thBorderStyle = 'solid';
			this.thBorderSize = '1px';
			this.thFontWeight = 'bold';

			// method
			this.getFirstDay = getFirstMonthDay;
			this.getNumDays = getMonthDays;
			this.setMonth = setNewMonth;
			this.setYear = setNewYear;
			this.create = monthCreate;
		}

		function setNewMonth(month){
			this.month = month;
		}

		function setNewYear(year){
			this.year = year;
		}

		// returns the number of days in a month
		function setNumDays(month, year){
			if (month == 1){		// February
				numDays = (year % 4 == 0 ? 29 :  28);
			}							// 31 day months
			else if (month == 0 || month == 2 || month== 4
					|| month == 6 || month == 7 || month == 9
					|| month == 11)
				numDays = 31;
			else 						// 30 day months
				numDays = 30;

			return numDays;
		}

		// returns the day of the week of the first of a month
		function setFirstMonthDay(month, year){
			var aDay = new Date(year, month, 1);
			return (aDay.getDay());
		}

		// return the number of days in a month
		function getMonthDays(){
			return this.numDays;
		}

		function getFirstMonthDay(){
			return this.firstMonthDay;
		}

		function monthCreate(){

			var m_str = '';
			var dayCounter = 1;
			var spaceCounter = 0;

			var dayPerWeek = 7;
			var spacePerMonth = 42;
			var numMonth = this.month+1; // actual numerical month is 1+month

			m_str  += '<table id=month cellspacing=1 cellpadding=0>'
				+ '<tr>'
				+ '<th> S </th>'
				+ '<th> M </th>'
				+ '<th> T </th>'
				+ '<th> W </th>'
				+ '<th> Th </th>'
				+ '<th> F </th>'
				+ '<th> S </th>'
				+ '</tr>';

			if (this.firstMonthDay != 0)
				m_str += '<tr bgcolor=white>';

			// skip the empty days before 1st of the month
			for (spaceCounter = 0; spaceCounter < this.firstMonthDay; spaceCounter++){
				m_str += '<td> &nbsp; </td>';
			}

			// write out the days
			while (dayCounter <= this.numDays){
				if (spaceCounter % dayPerWeek == 0)
					m_str += '<tr>';

				m_str += ' <td  bgcolor=white>';
				m_str += ' <a href="#" class='+getDayClass(this.year, this.month, dayCounter)+' onClick="handleDate('+dayCounter+', '+numMonth+', '+this.year+')"> ';
				m_str += dayCounter;
				m_str += ' </a></td>';

				if (spaceCounter % dayPerWeek == 6)
					m_str+= '</tr>';

				dayCounter++;
				spaceCounter++;
			}

			// write out the rest of the empty space at the end of the calendar
			while (spaceCounter < spacePerMonth){
				m_str += '<td  bgcolor=white> &nbsp; </td>';

				if (spaceCounter % dayPerWeek == 6){
					m_str += '</tr>';
				}

				if (spaceCounter % dayPerWeek == 6 && spaceCounter != (spacePerMonth - 1)){
					m_str += '<tr>';
				}
				spaceCounter++;
			}

			m_str += '</table>';

			return m_str;
		}

		function getDayClass(year, month, day){
			var className = '';
			var oDay = new Date(year, month, day);

			if (oDay < TODAY)
				className = 'pastDay';

			if (oDay.getYear() == TODAY_YEAR && oDay.getMonth() == TODAY_MONTH && oDay.getDate() == TODAY_DATE)
				className = 'toDay';

			if (oDay > TODAY)
				className = 'futureDay';

			return className;
		}

// Tool bar object ***************************************************/
		function ToolBar(month, year){
			this.month = month;
			this.year = year;
			this.monthName = setMonthName(month);

			this.setMonth = setNewMonth;
			this.setYear = setNewYear;
			this.getName = getMonthName;
			this.create = createToolBar;
		}

		function setNewMonth(month){
			this.month = month;
		}

		function setNewYear(year){
			this.year = year;
		}

		// return the Name of the month
		function getMonthName(){
			return this.monthName;
		}

		function setMonthName(month){

			switch (month){
			case 0: monthName = 'January';
			break;

			case 1: monthName = 'February';
			break;

			case 2: monthName = 'March';
			break;

			case 3: monthName = 'April';
			break;

			case 4: monthName = 'May';
			break;

			case 5: monthName = 'June';
			break;

			case 6: monthName = 'July';
			break;

			case 7: monthName = 'August';
			break;

			case 8: monthName = 'September';
			break;

			case 9: monthName = 'October';
			break;

			case 10: monthName = 'November';
			break;

			case 11: monthName = 'December';
			break;

			default: monthName = 'Error';
			break;
			}
			return monthName;
		}

		// draw the tool bar, return only the str.
		function createToolBar(){

			var str = '';
			str  = '<table name=toolBar id=toolBar cellspacing=3 cellpadding=0 align=center>';
			str += '<tr>';
			str += '<td width="5%"> <a href="#" class=button onClick="mouseClickBack('+this.month+', '+this.year+'); return false;"> <img src="../images/arrow1_w.gif" border=0> </a> </td>';
			str += '<td width="90%" nowrap>'+this.monthName+' '+this.year+'</td>';
			str += '<td width="5%"> <a href="#" class=button onClick="mouseClickForward('+this.month+', '+this.year+'); return false;"> <img src="../images/arrow1_e.gif" border=0> </a></td>';
			str += '</tr>';
			str += '</table>';

			return (str);
		}

// handle events for ToolBar *****************************************************/
		function mouseClickBack(month, year){
			if (month == 0){
				month = 11;
				--year;
			}else{
				--month;
			}

			updateMouseClick(month, year);
		}

		function mouseClickForward(month, year){
			if (month == 11){
				month = 0;
				++year;
			}else{
				++month;
			}

			updateMouseClick(month, year);
		}

		function updateMouseClick(month, year){
			var oCalendar = new Calendar(month, year);
			oCalendar.create();
		}

// handle events on dates**********************************************************/

		// writes the date in the input field
		function handleDate(day, month, year){
			var inputID = self.name;
			obj = window.opener.document.getElementById(inputID);
			if ( obj ) 
				obj.value = year + '-' + month + '-' + day;
//			if (obj)
//				obj.value = setMonthName(month-1) + ' ' + day + ', ' + year; // month+'/'+day+'/'+year ;
			window.close();
		}

		// opens a calendar window passing name as the name of the field to be filled in
		function openCalendar(obj, name, calendarHtml){
			
			var width = 160;
			var height = 148;
			var CalendarName = name;
			
			if ( event != null  ) {
				var xpos = event.screenX;
				var ypos = event.screenY;
			}  else {
				var xpos = 0;
				var ypos = 0;
			}
			
			if ((screen.availWidth - xpos) < width){
				xpos = (xpos - width);
			}

			if ((screen.availHeight - ypos) < height){
						ypos = (ypos - height);
			}
			
			
			window.open(calendarHtml, name, 'width='+width+'; height='+height+'; resizable=yes; left='+xpos+'; top='+ypos);
		}

// maintain calendar in focus************************************************************/
		var KeepFocus = true;


		function maintainFocus() {
			if(KeepFocus == true)
				self.focus();

			if( window.opener.closed){
				self.close();
			}

			setTimeout("maintainFocus()",2000);
		}



